package com.example.controller.before;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.Goods;
import com.example.service.GoodsService;
import com.example.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "小程序物品")
@CrossOrigin
@RequestMapping("/goodsBefore")
@RestController
public class GoodsBeforeController {

    @Autowired
    private GoodsService goodsService;

    @ApiOperation(value = "查询捐赠物品")
    @GetMapping("getDonaGoods/{current}/{limit}")
    public R getDonaGoods(
            @ApiParam(name = "current", value = "当前页", required = true) @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable long limit
    ) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.eq("is_donation", 1);//状态为 捐赠
        wrapper.eq("is_sell", 0);//状态为 未成交
        wrapper.eq("is_exhibit", 1);//状态为 审核通过的

        Page<Goods> page = new Page<>(current, limit);
        goodsService.page(page, wrapper);

        //获取分页后数据
        long total = page.getTotal();
        List<Goods> list = page.getRecords();
        return R.ok().data("total", total).data("list", list);
    }

    @ApiOperation(value = "查询需求物品")
    @GetMapping("getAccGoods/{current}/{limit}")
    public R getAccGoods(
            @ApiParam(name = "current", value = "当前页", required = true) @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable long limit
    ) {
        //状态为捐赠的物品
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.eq("is_donation", 0);//状态为 捐赠
        wrapper.eq("is_sell", 0);//状态为 未成交
        wrapper.eq("is_exhibit", 1);//状态为 审核通过的

        Page<Goods> page = new Page<>(current, limit);
        goodsService.page(page, wrapper);

        //获取分页后数据
        long total = page.getTotal();
        List<Goods> list = page.getRecords();
        return R.ok().data("total", total).data("list", list);
    }

    @ApiOperation(value = "添加捐赠物品")
    @PostMapping("addDonaGoods")
    public R addDonaGoods(@RequestBody Goods goods) {//title,author,goods_desc,picture
        if (!StringUtils.isEmpty(goods)) {
            boolean b = goodsService.save(goods);
            if (b) {
                return R.ok();
            } else {
                return R.error();
            }
        }
        return R.error().data("message", "物品信息为空");
    }

    @ApiOperation(value = "添加需求物品")
    @PostMapping("addAccGoods")
    public R addAccGoods(@RequestBody Goods goods) {//title,author,goods_desc,picture
        if (!StringUtils.isEmpty(goods)) {
            goods.setIsDonation(0);
            boolean b = goodsService.save(goods);
            if (b) {
                return R.ok();
            } else {
                return R.error();
            }
        }
        return R.error().data("message", "物品信息为空");
    }

    @ApiOperation(value = "搜索物品")
    @PostMapping("findGoods")
    public R findGoods(@RequestBody Goods goods) {
        String title = goods.getTitle();
        Integer isDonation = goods.getIsDonation();
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(title)) {
            wrapper.eq("is_exhibit", 1);
            wrapper.eq("is_sell", 0);
            if (!StringUtils.isEmpty(isDonation)) {
                wrapper.eq("is_donation", isDonation);
            }
            //根据作者，isbn，物品名字搜索
            wrapper.and(wr -> wr.like("author", title).or().like("isbn", title).or().like("title", title));
            List<Goods> list = goodsService.list(wrapper);
            return R.ok().data("list", list);
        }
        return R.error().message("goods为空");
    }


}
